#include <iostream>
#include <string>
#include <math.h>
using namespace std;

int is_prime(long unsigned int val)
{
    long unsigned int i = 3;

    long unsigned int max = (long unsigned int)sqrt((double)val);

    if (val % 2 == 0)
    {
        return 0;
    }

    while (i < max + 1)
    {
        if (val % i == 0)
        {
            return 0;
        }
        i += 1;
    }
    return 1;
}

int main()
{
    int primeCount = 0;
    int traverseCount = 0; // 质数一定在 6*traverseCount 的两侧,但在它两侧的不一定是质数
    int offset = 1;        //-1:左侧 1：右侧
    string s;
    while (1)
    {
        cout << "Do you want to see the next prime? (Y/N)" << endl;
        cin >> s;
        if (s == "n" || s == "N")
        {
            break;
        }
        else if (s == "y" || s == "Y")
        {
        }
        else
        {
            cout << "That is not an option" << endl;
            continue;
        }
        primeCount++;
        if (primeCount < 3)
        {
            cout << "the " << primeCount << " prime is:" << primeCount + 1 << endl;
            continue;
        }
        do
        {
            if (offset == 1)
            {
                offset = -1;
                traverseCount++;
            }
            else
            {
                offset = 1;
            }
        } while (is_prime(6 * traverseCount + offset) != 1);
        cout << "the " << primeCount << " prime is:" << 6 * traverseCount + offset << endl;
    }
    return 0;
}